package cn.bestick.weightrecord.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by IntelliJ IDEA.
 * User: Bestick
 * Date: 2016/7/14
 * Time: 0:49
 * Url: http://www.bestick.cn
 * Copyright © 2015-2016 Bestick All rights reserved
 * -----------------------------------------------------------
 * 会当凌绝顶，一览众山小。
 */
public class DateUtil {

    public static String formatDate(Date date, String format) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (date != null) {
            result = sdf.format(date);
        }
        return result;
    }

    /**
     * 转为西五区Unix时间戳
     * @param str 时间
     * @param format 格式化
     * @return String
     * @throws Exception
     */
    public static Timestamp formatString(String str, String format) throws Exception {
        if (StringUtil.isEmpty(str)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Long timestamp = sdf.parse(str).getTime();
//        System.out.println(">>>>>>>>>>>>>中国时间 " + sdf.parse(str).getTime());
//        System.out.println(">>>>>>>>>>>>>美国国时间 " + (sdf.parse(str).getTime() - 43200000L));
//        System.out.println(">>>>>>>>>>>>>中国时间 " + formatDate(new Date(timestamp + - 43200000L), "yyyy-MM-dd HH:mm:ss"));
//        System.out.println(">>>>>>>>>>>>>美国国时间 " + formatDate(new Date(timestamp), "yyyy-MM-dd HH:mm:ss"));
        return string2TimeStamp(formatDate(new Date(timestamp), "yyyy-MM-dd HH:mm:ss"));
    }

//    public static void main(String[] args) throws Exception {
//        String today = DateUtil.formatLong(System.currentTimeMillis(),"yyyy-MM-dd ");
//        String today1 = today + "06:30:00";
//        String today2 = today + "22:30:00";
//        System.out.println(today1);
//        System.out.println(today2);
//        System.out.println(formatString(today1,"yyyy-MM-dd HH:mm:ss"));
//        System.out.println(formatString(today2,"yyyy-MM-dd HH:mm:ss"));
//    }


    /**
     * 显示为GMT+8时间
     *
     * @param date
     * @param format
     * @return
     * @throws Exception
     */
    public static String formatLong(Long date, String format) {
        if (StringUtil.isEmpty(Long.toString(date))) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        return sdf.format(date);
    }

    public static String getCurrentDateStr(String format) throws Exception {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Timestamp string2TimeStamp(String stringDate) {
        return Timestamp.valueOf(stringDate);
    }
}
